﻿using System;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;
using WssEvents;

/*
 *      3D合成游戏合成框架
 */
namespace JQMergeFramework
{
    
    //地块基类
    public class GroundBase : MonoBehaviour
    {

        public SpriteRenderer tipsGreen;
        
        //行
        public int row = 5;
        //列
        public int col = 5;

        public Vector3 mPos
        {
            get { return this.transform.position; }
        }

        private bool mIsDisabled = false;
        //棋盘格子是否是禁用状态
        public bool IsDisabled
        {
            get { return mIsDisabled; }
        }

        public CityUnit placeCity = null;


        public void SetDisabled(bool isDisabled)
        {
            mIsDisabled = isDisabled;
            this.gameObject.SetActive(!isDisabled);
        }

        private void Start()
        {
            ShowTipsGreen(false);
        }

        public void InitData(int _row,int _col)
        {
            row = _row;
            col = _col;
        }

        
        
        //更换格子绑定的城堡
        public void ChangeBindCity(CityUnit changeCity,bool isMovePath = false)
        {
            placeCity = changeCity;
            changeCity.BindGround(this,isMovePath);
        }
        
        
        
        public void OnRayClick()
        {
            //从队列取出，绑定城堡
            CityUnit city = TowerControl.I.DequeueUnitDataBase();
            
            //增加分数
            placeCity = city;
            
            city.BindGround(this);
            city.PutTrack();
            
            // Debug.Log("放置坐标："+this.gameObject.name);
            //检测一下是否可以合成
            MergeManager.Instance.RefreshGround(city);
            
            //重新生成城堡
            GlobalEvent.Dispatcher(GameEvents.OnCreateCity);
        }


        /// <summary>
        /// 清空绑定信息
        /// </summary>
        public void ClareCity()
        {
            placeCity = null;
        }


        //删除房子
        public void DestroyCity()
        {
            placeCity?.RDestroy();
            ClareCity();
        }

        //展示提升
        public void ShowTipsGreen(bool isShow)
        {
            if (tipsGreen.gameObject.activeSelf != isShow)
            {
                tipsGreen.gameObject.SetActive(isShow);
                
                if (isShow)
                {
                    tipsGreen.color = new Color(1, 1, 1, 0.8F);
                }
                else
                {
                    tipsGreen.DOKill();
                }
            }
        }

        //是否可以放置
        public bool isCanPlace()
        {
            return  placeCity == null;
        }

    }
}

